package com.dfec.utils;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;

public class MailUtil implements Runnable {
	private String email;// 收件人邮箱
	private String content;
	private String code;// 激活码
	private String subject;
	// 这个是你的邮箱用户名
	private String username = "wwp@honey.com";
	// 你的邮箱密码
	private String password = "123456";
	String from = "wwp@honey.com";// 发件人电子邮箱
	String host = "219.144.95.133"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)//邮件服务器地址

	

	public MailUtil(String  url,String email, String code, String password,String username, String type) {
		this.email = email;
		this.code = code;
		if (null!=password&&password.length()>0) {
			this.password=password;
		}
		if (null!=username&&username.length()>0) {
			this.username=username;
		}
		
		
		// type==1 发送激活邮件 type==2 发送随机重置密码邮件
		if (type.equals("1")) {
			subject = "【千村千群】账号激活";
			content = "<html><head></head><body><h1>这是一封激活邮件,激活请点击以下链接</h1><h3><a href='" +url + "/service/user/active?email=" + email + "&code=" + code + "'>"
					+ url+ "/service/user/active?email=" + email + "&code="
					+ code + "</href></h3><br><h1>【陇南市政府千村千群投票账号管理员激活邮件】</h1></body></html>";
		} else if (type.equals("2")) {
			subject = "【千村千群】重置密码";
			String newPwd = Token.makeToken(Token.DEFAULT_RANDOM_PASSWORD_LENGTH);
			content = "<html><head></head><body><h1>这是一封重置密码邮件,确认将密码重置为【" + newPwd + "】请点击以下链接</h1><h3><a href='"
					+ url+ "/service/user/resetpwd?pwd=" + newPwd
					+ "&email=" + email + "'>" +url
					+ "/service/user/resetpwd?pwd=" + newPwd + "&email=" + email
					+ "</href></h3><br><h1>【陇南市政府千村千群投票账号管理员重置密码邮件】</h1></body></html>";
		}
	}

	/**
	 * 用来进行服务器对用户的认证
	 */
	public class Email_Autherticator extends Authenticator {
		public Email_Autherticator() {
			super();
		}

		public Email_Autherticator(String user, String pwd) {
			super();
			username = user;
			password = pwd;
		}

		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	}

	public void run() {
		Properties properties = System.getProperties();// 获取系统属性
		properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
		properties.setProperty("mail.smtp.auth", "false");// 打开认证
		try {
			// QQ邮箱需要下面这段代码，163邮箱不需要
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			properties.put("mail.smtp.ssl.enable", "false");
			properties.put("mail.smtp.ssl.socketFactory", sf);
			// 1.获取默认session对象
			Session session = Session.getDefaultInstance(properties, new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("814884748@qq.com", "deamtjmzbcehbdeb"); // 发件人邮箱账号、授权码
				}
			});
			// 2.创建邮件对象
			Message message = new MimeMessage(session);
			// 2.1设置发件人
			message.setFrom(new InternetAddress(from));
			// 2.2设置接收人
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
			// 2.3设置邮件主题
			message.setSubject(subject);
			// 2.4设置邮件内容
			message.setContent(content, "text/html;charset=UTF-8");
			// 3.发送邮件
			Transport.send(message);
			System.out.println("邮件成功发送!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Thread(new MailUtil("","2840098916@qq.com" ,"sad kljld","23dasda","gfdhhdfs","2")).start();;
	}
}